﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace SynoManager.DownloadStation.Plugins
{
    [DownloadStationPlugin("Info Balloons", "Shows an infoballoon when a download has completed", "SynoManager Team", "1.0")]
    public class InfoBalloon : IDownloadStationPlugin
    {
        private DownloadStationService service;

        private bool enabled = Properties.Settings.Default.EnableInfoBalloons;

        public bool Enabled
        {
            get { return enabled; }
            set
            {
                enabled = value;
                Properties.Settings.Default.EnableInfoBalloons = value;
            }
        }

        public void Initialize(DownloadStationService service)
        {
            this.service = service;
            service.TaskStatusChanged +=service_TaskStatusChanged;
        }

        private DownloadTask task;
        void service_TaskStatusChanged(object sender, DownloadTaskEventArgs e)
        {
 	        if (enabled && e.Task.State == DownloadState.Completed)
 	        {
 	            var notifyIcon = service.Plugin.Host.NotifyIcon;
 	            task = e.Task;
 	            var text = "The following file has been successfully downloaded to your NAS:\n"+task.Filename;
                notifyIcon.ShowBalloonTip(4000, "Download Complete", text, ToolTipIcon.Info, notifyIcon_BalloonTipClicked);                
 	        }
        }

        void notifyIcon_BalloonTipClicked(object sender, EventArgs e)
        {
            Debug.Assert(task != null);
            try
            {
                task.OpenFile();
            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show(ex.Message, "Open Task", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Close()
        {
            this.service = null;
        }
    }
}
